package com.fzrskj.cloud.free.sys.dao;

import com.fzrskj.cloud.free.core.tool.CObjectTool;
import com.fzrskj.cloud.free.core.utils.CloudContextUtils;
import com.fzrskj.cloud.free.sys.bean.SysMiniServiceBean;
import ms.core.consts.SysErr;
import ms.core.db.DbUtilsDrv;
import ms.core.db.IDbDrv;
import ms.core.object.CObject;
import ms.core.tool.StrTool;

import java.util.List;
import java.util.Map;
import java.util.ArrayList;

public class SysMiniServiceDao extends CObject {
    //private declarations
    private IDbDrv db = null;

    //constructor function to initialize database access object
    public SysMiniServiceDao() {
        db = new DbUtilsDrv();
    }

    //to free resource
    @Override
    public void dispose() {
        if (db != null) {
            db.closeConnection();
        }
        db = null;
        super.dispose();
    }

    //new record to database by map
    public int newSysMiniService(Map<String, Object> values) {
        //代理商数据隔离
        values.put("agentId", CloudContextUtils.DEF_AGENT_ID);
        int ret = db.insertRecord("sys_mini_service", values);
        String err = db.getErrCode();
        if (!err.equals(SysErr.CODE_SUCCESS)) {
            this.log(db.getMsg());
        }

        this.setErrCode(err);
        return ret;
    }

    //to delete table record by id
    public int delSysMiniService(int id) {
        String sql = "delete from sys_mini_service where id=?";
        Object[] params = {id};
        //代理商数据隔离
        sql += " and agentId=? ";
        params = CObjectTool.objectArrAddValue(params, CloudContextUtils.DEF_AGENT_ID);

        int ret = db.executeSql(sql, params);
        String err = db.getErrCode();
        if (!err.equals(SysErr.CODE_SUCCESS)) {
            this.log(db.getMsg());
        }

        this.setErrCode(err);
        return ret;
    }

    //to delete table record by cdt
    public int delSysMiniService(Map<String, Object> cdt) {
        //代理商数据隔离
        cdt.put("agentId", CloudContextUtils.DEF_AGENT_ID);

        int ret = db.deleteRecord("sys_mini_service", cdt);
        String err = db.getErrCode();
        if (!err.equals(SysErr.CODE_SUCCESS)) {
            this.log(db.getMsg());
        }

        this.setErrCode(err);
        return ret;
    }

    //to modify table record by values and condition
    public int modSysMiniService(Map<String, Object> values, Map<String, Object> cdt) {
        //代理商数据隔离
        cdt.put("agentId", CloudContextUtils.DEF_AGENT_ID);

        int ret = db.modifyRecord("sys_mini_service", values, cdt);
        String err = db.getErrCode();
        this.setErrCode(err);
        if (!err.equals(SysErr.CODE_SUCCESS)) {
            this.log(db.getMsg());
        }

        return ret;
    }

    //to query sys_mini_service item for detail
    public SysMiniServiceBean querySysMiniService(int id) {
        String sql = "select * from sys_mini_service where id=?";

        Object[] params = {id};
        //代理商数据隔离
        sql += " and agentId=? ";
        params = CObjectTool.objectArrAddValue(params, CloudContextUtils.DEF_AGENT_ID);

        SysMiniServiceBean ret = db.queryForBean(sql, SysMiniServiceBean.class, params);
        String err = db.getErrCode();
        if (!err.equals(SysErr.CODE_SUCCESS)) {
            this.log(db.getMsg());
        }
        this.setErrCode(err);
        return ret;
    }

    //to query SysMiniService record count
    public int querySysMiniService(Map<String, Object> cdt) {
        //代理商数据隔离
        cdt.put("agentId", CloudContextUtils.DEF_AGENT_ID);

        int ret = db.queryRecordCount("sys_mini_service", cdt);
        this.setErrCode(db.getErrCode());
        return ret;
    }

    //to query SysMiniServiceBean for record list with condition,if page or pagesize less than 0, means without page
    public List<SysMiniServiceBean> querySysMiniService(Map<String, Object> cdt, String orderby, int page, int pagesize) {
        //代理商数据隔离
        cdt.put("agentId", CloudContextUtils.DEF_AGENT_ID);

        List<SysMiniServiceBean> rets = db.queryForBeans("sys_mini_service", "*", orderby, SysMiniServiceBean.class, cdt, page, pagesize);
        String err = db.getErrCode();
        if (!err.equals(SysErr.CODE_SUCCESS)) {
            this.log(db.getMsg());
        }

        this.setErrCode(err);
        return rets;
    }

    public List<SysMiniServiceBean> querySysMiniService(Map<String, Object> cdt, String orderBy) {
        //代理商数据隔离
        cdt.put("agentId", CloudContextUtils.DEF_AGENT_ID);

        List<Object> params = new ArrayList<>();
        String sql = "select * from sys_mini_service where 1=1 ";

        for (Map.Entry<String, Object> c : cdt.entrySet()) {
            sql = sql + " and " + c.getKey() + "=?";
            params.add(c.getValue());
        }
        if (!StrTool.isBlankStr(orderBy)) {
            sql += " order by " + orderBy;
        }
        return this.db.queryForBeans(sql, SysMiniServiceBean.class, params);
    }

    /**
     * 根据编码查询指定id外的记录
     *
     * @param serviceCode 服务编码.
     * @param id          id.
     * @return 服务信息记录.
     */
    public SysMiniServiceBean getByCodeWithoutId(String serviceCode, Integer id) {
        String sql = "select * from sys_mini_service where serviceCode=? and id!=? and agentId=? limit 1";
        Object[] params = {serviceCode, id};
        //代理商数据隔离
        params = CObjectTool.objectArrAddValue(params, CloudContextUtils.DEF_AGENT_ID);
        return this.db.queryForBean(sql, SysMiniServiceBean.class, params);
    }
}